package com.ganbo.rabbitmq.ttl;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * 死信队列
 *
 * @Author: ganbo
 * @Date: 2020/12/8 20:30
 */

@Configuration
public class DLConfig {

    //配置一个队列作为死信队列
    @Bean("dlQueue")
    public Queue dlQueue() {
        return new Queue("dlQueue", true, false, false, new HashMap<>());
    }


    //配置一个交换机作为死信交换机
    @Bean("dlExchange")
    public TopicExchange dlExchange() {
        return new TopicExchange("dlExchange", true, false, new HashMap<>());
    }

    //绑定交换机和队列
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(dlQueue()).to(dlExchange()).with("#");
    }

    //配置一个交换机作为普通交换机
    @Bean("putongExchange")
    public TopicExchange putongExchange() {
        return new TopicExchange("putongExchange", true, false, new HashMap<>());
    }

    //创建一个普通队列
    @Bean("putongQueue")
    public Queue putongQueue() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-message-ttl", 10000); // 10秒钟后成为死信
        map.put("x-dead-letter-exchange", "dlExchange"); // 队列中的消息变成死信后，进入死信交换机
        return new Queue("putongQueue", true, false, false, map);
    }

    //绑定普通队列和普通交换机
    @Bean
    public Binding putongBinding() {
        return BindingBuilder.bind(putongQueue()).to(putongExchange()).with("#");
    }


    @Bean
    public Queue putongQueue2() {
        return new Queue("putongQueue2", true, false, false);
    }


}
